package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.TxnLimitCfgMapper;
import cc.rengu.igas.bsps.common.entity.TxnLimitCfg;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class TxnLimitCfgMapperImpl implements TxnLimitCfgMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public TxnLimitCfg selectTxnLimitCfgByPrimaryKey(String instId, String limitType, String limitInst, String limitAcctType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnLimitCfgByPrimaryKey");
        TxnLimitCfg txnLimitCfg = new TxnLimitCfg();
        txnLimitCfg.setInstId(instId);
        txnLimitCfg.setLimitType(limitType);
        txnLimitCfg.setLimitInst(limitInst);
        txnLimitCfg.setLimitAcctType(limitAcctType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnLimitCfg, txnLimitCfg);
        if (0 != iReturnCode) {
            rglog.error("获取交易限额配置信息失败,instId:<{}>,limitType:<{}>,limitInst:<{}>,limitAcctType:<{}>,iReturnCode:<{}>", instId, limitType, limitInst, limitAcctType, iReturnCode);
            return null;
        }
        return txnLimitCfg;
    }

    @Override
    public List<TxnLimitCfg> selectTxnLimitCfgByLimitInst(String instId, String limitInst) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnLimitCfgByMchntNo");
        TxnLimitCfg txnLimitCfg = new TxnLimitCfg();
        txnLimitCfg.setInstId(instId);
        txnLimitCfg.setLimitInst(limitInst);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnLimitCfg, TxnLimitCfg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取交易限额配置信息失败,instId:<{}>,limitInst:<{}>", instId, limitInst);
            return null;
        }
        return objectList.stream().map(item -> (TxnLimitCfg) item).collect(Collectors.toList());
    }
}
